/*
 * ChatPanelTest.java
 *
 * Created on September 21, 2008, 11:16 PM
 */
package no.eirikb.bomberman.applet.test;

import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import no.eirikb.bomberman.applet.Bomberman;
import no.eirikb.bomberman.applet.game.GamePanel;
import no.eirikb.bomberman.client.BombermanClient;
import no.eirikb.bomberman.server.Server;
import no.eirikb.bomberman.shared.Game;
import no.eirikb.bomberman.shared.User;

/**
 *
 * @author  eirikb
 */
public class GamePanelTest2 extends javax.swing.JFrame {

    private Server server;
    private Double hash;
    private String nick;

    /** Creates new form ChatPanelTest */
    public GamePanelTest2(int id) {
        try {
            initComponents();
            setLocation(640 * id, 0);
            Bomberman bomberman = new Bomberman();
            BombermanClient client = new BombermanClient(bomberman);
            server = (Server) Naming.lookup(bomberman.getHOST());
            nick = Math.random() + "";
            User user = new User(nick);
            System.out.println("User: " + user.getNick());
            hash = server.registerUser(client, user);
            if (hash != null) {
                bomberman.setClient(client);
                bomberman.setHash(hash);
                bomberman.setUser(user);
                bomberman.setIdb(server.getImageDB());
                bomberman.getIdb().generateImages();
                bomberman.setServer(server);
                GamePanel gamePanel = new GamePanel(bomberman);
                client.setGamePanel(gamePanel);
                add(gamePanel, BorderLayout.CENTER);

                // Create a testgame if the user is the first
                if (id == 0) {
                    Game game2 = new Game("testgame", 2, 10, 10);
                    game2.setStartSpeed(3);
                    game2.setStartBombSize(3);
                    server.createGame(hash, user.getNick(), game2);
                } else {
                    server.joinGame(hash, user.getNick(), "testgame");
                }

                server.setReady(hash, user.getNick(), true);
            } else {
                System.out.println("HASH IS NULL!");
            }
        } catch (NotBoundException ex) {
            Logger.getLogger(GamePanelTest2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(GamePanelTest2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(GamePanelTest2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(640, 480));
        setName("ChatPanelTest"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    try {
        server.disconnect(hash, nick);//GEN-LAST:event_formWindowClosing
    } catch (RemoteException ex) {
        Logger.getLogger(GamePanelTest2.class.getName()).log(Level.SEVERE, null, ex);
    }
}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
             //   new GamePanelTest2(0).setVisible(true);
                new GamePanelTest2(1).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
